package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.CouponApi;
import com.kgc.sbt.api.GoodsApi;
import com.kgc.sbt.domain.Coupon;
import com.kgc.sbt.mapper.CouponMapper;
import com.kgc.sbt.util.RequestResult;
import com.kgc.sbt.util.ResultBuildUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Shan
 * @date 2021/6/22
 * @return
 * @description
 */

@Service
@Slf4j
public class CouponProvider implements CouponApi {

    @Autowired
    private CouponMapper couponMapper;

    @Reference
    private GoodsApi goodsApi;

    @Override
    public Coupon queryCouponById(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Coupon> queryAllCoupon() {
        return couponMapper.selectByExample(null);
    }

    //改库存
    @Override
    public boolean updateCouponCountById(Coupon coupon) {
        return couponMapper.updateByPrimaryKey(coupon) == 1 ? true : false;
    }

    @Override
    public boolean compareUsedAndCid(Integer couponId, Integer goodsId) {
        log.error("compareUsedAndCid({}, {})", couponId, goodsId);
        int used = couponMapper.selectByPrimaryKey(couponId).getUsed();
        if (used == 0 || used == goodsApi.queryGoodsById(goodsId).getCId()) {
            return true;
        }
        return false;
    }
}
